import React, {Component} from 'react';
import Album from './home/album/Album';
import {Button} from '@ant-design/react-native';
import {upload, getImageFile} from '~/utils/request';
import {Alert, View} from 'react-native';

export default class demo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            files: [],
        };
    }

    setFiles = (res) => {
        let files = [];
        res.forEach((file) => {
            files.push(getImageFile(file));
        });
        this.setState({
            files,
        });
    };

    onUpload = () => {
        upload('/Upload/UploadTest', this.state.files, {fileid: '123'}).then((r) => {
            if (r.code === 0) {
                Alert.alert('图片' + r.data.file + '上传成功！');
            } else {
                Alert.alert('上传失败，原因：' + r.msg);
            }
        });
    };
    render() {
        return (
            <View>
                <Album onSelect={this.setFiles} />
                <Button onPress={this.onUpload}>上传</Button>
            </View>
        );
    }
}
